//
//  VisitorLoginView.swift
//  weibo22
//
//  Created by keshuaiguo on 16/1/17.
//  Copyright © 2016年 keshuaiguo. All rights reserved.
//

import UIKit
import SnapKit

//协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //协议方法
    //用户将要注册
    optional func userWillRegister()
    
    //用户将要登陆
    func userWillLogin()
}


class VisitorLoginView: UIView {
    
    //声明代理 swift属性 默认的属性关键字 就是 strong
    //声明弱引用的属性 需要 weak
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    
    //MARK: 按钮的监听事件方法
    @objc private func loginBtnDidClick() {
        //使用代理调用协议方法
        
        //if (代理 != nil)
        visitorViewDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick() {
        // if([代理 responseSelxxxx])
        visitorViewDelegate?.userWillRegister?()
    }
    
    //外界调用的重要方法
    func setupUIWithInfo(imageName: String?,tipText: String) {
        if let img = imageName {
            circleIcon.image = UIImage(named: img)
            //隐藏大房子
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        } else {
            //说明在首页
            startAnimation()
            
        }
        
        tipLabel.text = tipText
    }
    //圈圈的动画效果
    func startAnimation() {
        //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        //设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        //持续时间
        base.duration = 10
        //设置动画完成/ 页面失去活跃状态  不移除图层
        base.removedOnCompletion = false
        //添加动画
        circleIcon.layer.addAnimation(base, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    
    //默认实现报错
    //当程序员通过 sb / xib 加载该视图 程序就会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加子视图
        addSubview(circleIcon)
        //为了遮住圈圈
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        
        /*
        添加约束 推荐 使用链式约束
        */
        //snp_makeConstraints 表示添加一条约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            //垂直居中 添加偏移量
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            //宽度约束
            make.width.equalTo(225)
            //为了防止页面跳动
            make.height.equalTo(40)
            
        }
        
        //登陆按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backView.snp_makeConstraints { (make) -> Void in
            //            make.top.equalTo(self.snp_top)
            //            make.left.equalTo(self.snp_left)
            //            make.right.equalTo(self.snp_right)
            //和Masory 类似 支持链式设置约束的方式
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        
        //灰度颜色
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加监听事件
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)

        
    }

    
    //懒加载获取所有的子视图
    //背景
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示文案
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(14)
        l.text = "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微"
        //设置行数
        l.numberOfLines = 0
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        //设置大小
        l.sizeToFit()
        return l
    }()
    
    //登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        //设置按钮的文字
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        return btn
    }()
    
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //设置按钮的文字
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        return btn
    }()
    
}


